﻿using System;
using System.Collections.Generic;
using System.Net.Sockets;

namespace JPR.Common.Objects
{
	public class SocketAsyncEventArgsPool
	{
		private readonly Stack<SocketAsyncEventArgs> _pool;

		public SocketAsyncEventArgsPool()
		{
			_pool = new Stack<SocketAsyncEventArgs>();
		}

		public int Count
		{
			get { return _pool.Count; }
		}

		public void Push(SocketAsyncEventArgs item)
		{
			if (item == null)
			{
				throw new ArgumentNullException("item");
			}
			lock (_pool)
			{
				_pool.Push(item);
			}
		}

		public SocketAsyncEventArgs Pop()
		{
			lock (_pool)
			{
				return _pool.Pop();
			}
		}

		public bool TryPop(out SocketAsyncEventArgs sae)
		{
			sae = null;
			lock (_pool)
			{
				if (_pool.Count > 0)
				{
					sae = _pool.Pop();
				}
			}
			return sae != null;
		}
	}
}